Imports System.IO
Imports System
Public Class frmTABLEsimple
    Dim Tbl_Action As Integer = 0
    '0: Nothing
    '1: Move
    '2: Split
    '3: Group
    Dim FromTBL_CODE As String = ""
    Dim ToTBL_CODE As String = ""
    Dim FromBILL_CODE As String = ""
    Dim ToBILL_CODE As String = ""
    Dim ArrTableGroup As ArrayList
    'Dim SelectedBillCode As Integer
#Region "Control Event"
    Private Sub btnManagement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnManagement.Click
        Dim frm As New frmMANAGEMENT
        frm.ShowDialog()
    End Sub

    Private Sub frmTABLE_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Try
            GC.Collect()
            Me.Dispose()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub frmTABLE_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PublicFunction.NewFormLoad(Me)
        Dim arName As New ArrayList
        arName.Add("@STORE_CODE")
        Dim arVal As New ArrayList
        arVal.Add(PublicVariable.POSStoreCode)

        Dim dtTable As DataTable = BLL.ObjectGetBy("FLOOR_LOADALL", arName, arVal)
        GridFloor.DataSource = dtTable

        If GridFloor.RowCount > 0 Then
            GridFloor.Row = 0
        End If
        If File.Exists(PublicVariable.LogoFile) Then
            PbLogo.BackgroundImage = New Bitmap(PublicVariable.LogoFile)
        End If
    End Sub

    Private Sub GridFloor_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridFloor.SelectionChanged
        Dim FilePath As String = Application.StartupPath + "\Images\" + GridFloor.GetValue("Image").ToString
        If File.Exists(FilePath) Then
            pnTable.InnerContainerFormatStyle.BackgroundImage = New Bitmap(FilePath)
        End If
        RefreshTable()
    End Sub
    Private Sub btnMoveJoin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveJoin.Click
        Tbl_Action = 1
    End Sub
    Private Sub btnLink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub
    Private Sub btnSplit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSplit.Click
        Tbl_Action = 2
    End Sub
#End Region
#Region "Functions"
    Private Sub RefreshTable()
        Dim arName As New ArrayList
        arName.Add("@FL_CODE")
        arName.Add("@DATE")
        Dim arVal As New ArrayList
        arVal.Add(GridFloor.GetValue("FL_CODE").ToString)
        arVal.Add(PublicVariable.SystemDate)


        Dim dtTable As DataTable = BLL.ObjectGetBy("TABLES_LOADBYFLOOR", arName, arVal)
        Dim dv As New DataView(dtTable)
        'PanelContainer.Controls.Clear()
        'For i As Integer = 0 To dv.Count - 1
        '    Dim tmp As New uscTABLE
        '    tmp.Top = dv.Item(i).Item("POSX")
        '    tmp.Left = dv.Item(i).Item("POSY")
        '    tmp.Width = dv.Item(i).Item("TBLW")
        '    tmp.Height = dv.Item(i).Item("TBLH")
        '    tmp.TBL_Code = dv.Item(i).Item("TBL_Code")
        '    tmp.NoOfBill = dv.Item(i).Item("NoOfBill")
        '    tmp.TBL_NAME = dv.Item(i).Item("TBL_Name")
        '    tmp.lblTABLE_NAME.Text = dv.Item(i).Item("TBL_Name")
        '    tmp.GroupCode = dv.Item(i).Item("GROUP_CODE")
        '    tmp.ResID = dv.Item(i).Item("ID")
        '    tmp.Deposit = dv.Item(i).Item("RES_DEPOSIT")

        '    Select Case tmp.NoOfBill
        '        Case 0 'FREE
        '            If tmp.ResID > 0 Then
        '                tmp.BackgroundImage = My.Resources.TBL_RESER
        '            Else
        '                tmp.BackgroundImage = My.Resources.TBL_FREE
        '            End If

        '        Case Else 'MULTI BILL
        '            tmp.BackgroundImage = My.Resources.TBL_BUSY
        '    End Select
        '    PanelContainer.Controls.Add(tmp)
        '    AddHandler tmp.Click, AddressOf Table_Click
        'AddHandler tmp.MouseHover, AddressOf Table_MouseHover
        'AddHandler tmp.MouseLeave, AddressOf Table_MouseLeave
        'Next
    End Sub
    Private Sub Table_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)
        'PanelContainer.Controls.Remove(PublicVariable.pnToolTipTable)
    End Sub
    Private Sub Table_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
        PublicVariable.pnToolTipTable = New uscToolTip
        PublicVariable.pnToolTipTable.TableID = CType(sender, uscTABLE).TBL_Code
        'PanelContainer.Controls.Add(PublicVariable.pnToolTipTable)
        Dim c As uscTABLE = CType(sender, uscTABLE)
        PublicVariable.pnToolTipTable.Top = Cursor.Position.Y
        PublicVariable.pnToolTipTable.Left = Cursor.Position.X
    End Sub
    Private Sub Table_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim CurrentTbl As New uscTABLE
        CurrentTbl = CType(sender, uscTABLE)
        Select Case Tbl_Action
            Case 0 'NOTHING
                If CurrentTbl.NoOfBill > 1 Then
                    'LOAD NHIEU BILL
                    Dim frm As New frmMULTI_BILL
                    frm.TBL_CODE = CurrentTbl.TBL_Code
                    If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        'LOAD SELECTED BILL TO SCREEN
                        Dim frm1 As New frmORDER
                        frm1.BillCode = frm.GridMultiBill.GetValue("BILL_CODE")
                        'frm1.LoadBillDetail(frm.GridMultiBill.GetValue("BILL_CODE"))
                        frm1.txtTBL_NAME.Text = CurrentTbl.TBL_NAME
                        frm1.ShowDialog()
                    End If
                ElseIf CurrentTbl.NoOfBill = 1 Then
                    ' LOAD 1 BILL
                    Dim frm As New frmORDER
                    Dim dt As DataTable
                    Dim arName As New ArrayList
                    Dim arVal As New ArrayList
                    arName.Add("@TBL_CODE")
                    arVal.Add(CurrentTbl.TBL_Code)
                    dt = BLL.ObjectGetBy("BILL_LOADBYTABLE", arName, arVal)
                    frm.BillCode = dt.Rows(0).Item("BILL_CODE")
                    'frm.LoadBillDetail(oTable.LoadBill_ByTable(CurrentTbl.TBL_Code).Rows(0).Item("BILL_CODE"))
                    frm.txtTBL_NAME.Text = CurrentTbl.TBL_NAME
                    frm.ShowDialog()
                Else
                    'FREE
                    If CurrentTbl.ResID > 0 Then
                        If SystemFunction.ShowYesNoForm("THERE'S RESERVATION ON THIS TABLE, WOULD YOU LIKE TO CHECK-IN?", _
                            "DEPOSIT: " + String.Format(PublicVariable.NumberFormatString, CurrentTbl.Deposit)) = Windows.Forms.DialogResult.Yes Then
                            Dim arName As New ArrayList
                            arName.Add("@ID")
                            arName.Add("@USER")
                            arName.Add("@STATUS")
                            Dim arVal As New ArrayList
                            arVal.Add(CurrentTbl.ResID)
                            arVal.Add(PublicVariable.UserCode)
                            arVal.Add(2)
                            BLL.Object_Update_NonResult("RESERVATION_STATUS", arName, arVal)
                        End If
                    End If
                    Dim frm As New frmORDER
                    frm.TBL_CODE = CurrentTbl.TBL_Code
                    frm.BillDeposit = CurrentTbl.Deposit
                    frm.txtTBL_NAME.Text = CurrentTbl.TBL_NAME
                    frm.ShowDialog()
                End If
                RefreshTable()
            Case 1 'MOVE
                If FromTBL_CODE = "" Then
                    'FIRST SELECT
                    If CurrentTbl.NoOfBill > 1 Then
                        'SHOW MULTI BILL FOR SELECTION
                        Dim frm As New frmMULTI_BILL
                        frm.TBL_CODE = CurrentTbl.TBL_Code
                        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                            FromBILL_CODE = frm.GridMultiBill.GetValue("BILL_CODE")
                            FromTBL_CODE = CurrentTbl.TBL_Code
                        End If
                    ElseIf CurrentTbl.NoOfBill = 1 Then
                        '1 BILL
                        'CType(sender, uscTABLE).BackgroundImage = My.Resources.TBL_SELECT
                        CurrentTbl.BackgroundImage = My.Resources.TBL_SELECT
                        FromTBL_CODE = CurrentTbl.TBL_Code
                        Dim frm As New frmORDER
                        Dim dt As DataTable
                        Dim arName As New ArrayList
                        Dim arVal As New ArrayList
                        arName.Add("@TBL_CODE")
                        arVal.Add(FromTBL_CODE)
                        dt = BLL.ObjectGetBy("BILL_LOADBYTABLE", arName, arVal)
                        FromBILL_CODE = dt.Rows(0).Item("BILL_CODE")
                    Else
                        SystemFunction.ShowMessageForm("CAN'T MOVE EMPTY TABLE", "")
                        Tbl_Action = 0
                    End If
                    Exit Sub
                Else
                    'SECOND SELECT
                    Dim Action As Integer = 0
                    If CurrentTbl.NoOfBill >= 1 Then
                        'ASK MOVE OR JOIN

                        Select Case SystemFunction.ShowMoveJoinForm("DO YOU WANT TO MOVE OR JOIN?", "")
                            Case Windows.Forms.DialogResult.Yes 'MOVE
                                Action = 1
                            Case Windows.Forms.DialogResult.No 'JOIN
                                Action = 2
                            Case Else
                                Action = 0
                        End Select
                    Else : Action = 1
                    End If
                    ToTBL_CODE = CurrentTbl.TBL_Code
                    'MOVE/JOIN ACTION
                    Select Case Action
                        Case 0

                        Case 1 'MOVE
                            Dim arName As New ArrayList
                            Dim arVal As New ArrayList
                            arName.Add("@TBL_CODE")
                            arName.Add("@BILL_CODE")
                            arVal.Add(ToTBL_CODE)
                            arVal.Add(FromBILL_CODE)
                            BLL.Object_Update_NonResult("BILL_UPDATETABLE", arName, arVal)

                        Case 2 'JOIN
                            If CurrentTbl.NoOfBill > 1 Then
                                'SHOW MULTI BILL FOR SELECTION
                                Dim frm As New frmMULTI_BILL
                                frm.TBL_CODE = CurrentTbl.TBL_Code
                                If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                                    ToBILL_CODE = frm.GridMultiBill.GetValue("BILL_CODE")
                                End If
                            ElseIf CurrentTbl.NoOfBill = 1 Then
                                CurrentTbl.BackgroundImage = My.Resources.TBL_SELECT
                                Dim dt As DataTable
                                Dim arName1 As New ArrayList
                                Dim arVal1 As New ArrayList
                                arName1.Add("@TBL_CODE")
                                arVal1.Add(ToTBL_CODE)
                                dt = BLL.ObjectGetBy("BILL_LOADBYTABLE", arName1, arVal1)
                                ToBILL_CODE = dt.Rows(0).Item("BILL_CODE")
                            End If
                            Dim arName As New ArrayList
                            Dim arVal As New ArrayList
                            arName.Add("@MAIN_BILL_CODE")
                            arName.Add("@SUB_BILL_CODE")
                            arVal.Add(ToBILL_CODE)
                            arVal.Add(FromBILL_CODE)
                            BLL.Object_Update_NonResult("BILL_JOIN", arName, arVal)

                    End Select
                    FromTBL_CODE = ""
                    ToTBL_CODE = ""
                    Tbl_Action = 0
                    ToBILL_CODE = 0
                    FromBILL_CODE = 0
                End If
                RefreshTable()
            Case 2 'SPLIT
                Dim frm As New frmORDER
                frm.TBL_CODE = CurrentTbl.TBL_Code
                frm.txtTBL_NAME.Text = CurrentTbl.TBL_NAME
                frm.ShowDialog()
                Tbl_Action = 0
                RefreshTable()
            Case 3 'GROUP
                If CurrentTbl.GroupCode > 0 Then
                    If SystemFunction.ShowYesNoForm("DO YOU WANT TO UNGROUP THIS TABLE?", "") = Windows.Forms.DialogResult.Yes Then
                        Dim arName As New ArrayList
                        Dim arVal As New ArrayList
                        arName.Add("@TABLE_CODE")
                        arVal.Add(CurrentTbl.TBL_Code)
                        BLL.Object_Update_NonResult("BILLGROUP_DELETE", arName, arVal)
                        btnGroup_Click(sender, e)
                    Else
                        ToTBL_CODE = CurrentTbl.GroupCode
                        CurrentTbl.BackgroundImage = My.Resources.TBL_SELECT
                        'If ArrTableGroup.IndexOf(CurrentTbl.TBL_Code) >= 0 Then
                        '    ArrTableGroup.Remove(CurrentTbl.TBL_Code)
                        'Else
                        '    ArrTableGroup.Add(CurrentTbl.TBL_Code)
                        'End If
                    End If
                Else
                    CurrentTbl.BackgroundImage = My.Resources.TBL_SELECT
                    If ArrTableGroup.IndexOf(CurrentTbl.TBL_Code) >= 0 Then
                        ArrTableGroup.Remove(CurrentTbl.TBL_Code)
                    Else
                        If FromTBL_CODE = "" And ToTBL_CODE = "" Then
                            FromTBL_CODE = CurrentTbl.TBL_Code
                            'FIRST SELECT
                        Else
                            'SECOND SELECT
                            ArrTableGroup.Add(CurrentTbl.TBL_Code)
                        End If
                    End If

                End If
        End Select

    End Sub

    Private Sub CreateGroup()
        Dim GroupCode As Integer
        Dim TblCodeAdd As String = ""
        Dim arName As New ArrayList
        Dim arVal As New ArrayList
        If FromTBL_CODE <> "" Then
            arName.Clear()
            arVal.Clear()
            arName.Add("@MAIN_TABLE")
            arVal.Add(FromTBL_CODE)
            GroupCode = BLL.Object_Update_Result("BILLGROUP_ADD", arName, arVal)
            If GroupCode > 0 Then
                arName.Clear()
                arVal.Clear()
                arName.Add("@GROUP_CODE")
                arName.Add("@TABLE_CODE")
                arVal.Add(GroupCode)
                arVal.Add(FromTBL_CODE)
                BLL.Object_Update_NonResult("BILLGROUPDETAIL", arName, arVal)

                For i As Integer = 0 To ArrTableGroup.Count - 1
                    TblCodeAdd = ArrTableGroup.Item(i)
                    arName.Clear()
                    arName.Add("@GROUP_CODE")
                    arName.Add("@TABLE_CODE")
                    arVal.Clear()
                    arVal.Add(GroupCode)
                    arVal.Add(TblCodeAdd)
                    BLL.Object_Update_NonResult("BILLGROUPDETAIL", arName, arVal)
                Next
            End If
        Else
            For i As Integer = 0 To ArrTableGroup.Count - 1
                TblCodeAdd = ArrTableGroup.Item(i)
                arName.Clear()
                arName.Add("@GROUP_CODE")
                arName.Add("@TABLE_CODE")
                arVal.Clear()
                arVal.Add(ToTBL_CODE)
                arVal.Add(TblCodeAdd)
                BLL.Object_Update_NonResult("BILLGROUPDETAIL", arName, arVal)
                'oTable.ADD_GROUP_DETAIL(ToTBL_CODE, TblCodeAdd)
            Next
        End If
        FromTBL_CODE = ""
        ToTBL_CODE = ""
    End Sub
#End Region

    Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
        PublicFunction.GRID_UP_DOWN(0, GridFloor)
    End Sub

    Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click
        PublicFunction.GRID_UP_DOWN(1, GridFloor)
    End Sub

    Private Sub btnGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGroup.Click

        If btnGroup.StateStyles.FormatStyle.ForeColor = Color.Black Then
            'FIRST CLICK
            btnGroup.StateStyles.FormatStyle.ForeColor = Color.Red
            Tbl_Action = 3
            ArrTableGroup = New ArrayList
        Else
            CreateGroup()
            btnGroup.StateStyles.FormatStyle.ForeColor = Color.Black
            Tbl_Action = 0
            RefreshTable()
        End If
    End Sub

End Class